################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Experimental Data
#Table D 7
#
#Verena Fetscher
#July 2022
####################

####################
# Load packages
####################

library(tidyverse)
library(xtable)


##########################
#Load data file
##########################

load("dataLab_SocialInsurance_complete.Rda")



##########################
#Descriptive statistics
##########################


n<-c(as.numeric(table(is.na(df$age))/16)[1],
as.numeric(table(is.na(df$income_fam))/16)[1],
as.numeric(table(is.na(df$redistribution))/16)[1],
as.numeric(table(is.na(df$polint))/16)[1],
as.numeric(table(is.na(df$leftright))/16)[1],
as.numeric(table(is.na(df$instructions))/16)[1],
as.numeric(table(is.na(df$change))/16)[1],
as.numeric(table(is.na(df$numclicks))/16)[1],
as.numeric(table(is.na(df$gender))/16)[1],
as.numeric(table(is.na(df$religion))/16)[1])

df %>%
  summarize(age = min(age,na.rm=T),
            income_fam=min(income_fam,na.rm=T),
            redistribution=min(redistribution,na.rm=T),
            polint=min(polint,na.rm=T),
            leftright=min(leftright,na.rm=T),
            instructions=min(instructions,na.rm=T),
            change=min(change,na.rm=T),
            clicks=min(numclicks,na.rm=T),
            gender=min(gender, na.rm=T),
            religion=min(religion,na.rm=T)
  ) -> descr_min

df %>%
  summarize(age = max(age,na.rm=T),
            income_fam=max(income_fam,na.rm=T),
            redistribution=max(redistribution,na.rm=T),
            polint=max(polint,na.rm=T),
            leftright=max(leftright,na.rm=T),
            instructions=max(instructions,na.rm=T),
            change=max(change,na.rm=T),
            clicks=max(numclicks,na.rm=T),
            gender=max(gender, na.rm=T),
            religion=max(religion,na.rm=T)
  ) -> descr_max

df %>%
  summarize(age = mean(age,na.rm=T),
            income_fam=mean(income_fam,na.rm=T),
            redistribution=mean(redistribution,na.rm=T),
            polint=mean(polint,na.rm=T),
            leftright=mean(leftright,na.rm=T),
            instructions=mean(instructions,na.rm=T),
            change=mean(change,na.rm=T),
            clicks=mean(numclicks,na.rm=T),
            gender=mean(gender, na.rm=T),
            religion=mean(religion,na.rm=T)
  ) -> descr_mean


df %>%
  summarize(age = sd(age,na.rm=T),
            income_fam=sd(income_fam,na.rm=T),
            redistribution=sd(redistribution,na.rm=T),
            polint=sd(polint,na.rm=T),
            leftright=sd(leftright,na.rm=T),
            instructions=sd(instructions,na.rm=T),
            change=sd(change,na.rm=T),
            clicks=sd(numclicks,na.rm=T),
            gender=sd(gender, na.rm=T),
            religion=sd(religion,na.rm=T)
  ) -> descr_sd

dfDescr<-data.frame(rbind(n,descr_min, descr_max,descr_mean,descr_sd))
data.frame(dfDescr)
rownames(dfDescr)<-c("N","Min","Max","Mean","SD")
colnames(dfDescr)<-c("Age","Family income","Redistribution","Interested in politics","Leftright",
                     "Instructions","Make changes in transfer decisions","Number of clicks (transfer decision)","Female",
                     "Member religious community")


##########################
#Table D.7: Descriptive statistics. Means, standard deviations, and percentages.
##########################
xtable(t(dfDescr), caption="Descriptive Statistics. Means, standard deviations, and percentages.")


